package com.ylt.juc;

import java.util.TreeMap;

public class SaleTicketDemo01 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(new MyThread()).start();
//        //并发，多线程操作同一个资源类
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 60; i++) {
//                    ticket.sale();
//                }
//            }
//        },"A").start();

        //lambda表达式(参数)->{代码}
        new Thread(()->{
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        },"C").start();
    }
}

class MyThread implements Runnable {
    @Override
    public void run() {

    }
}

// 资源类 OOP
class Ticket {
    //属性，方法
    private int ticketNum = 50;

    //买票方式
    public synchronized void sale(){
        if(ticketNum>0){
            System.out.println(Thread.currentThread().getName()
            +"卖出了"+(51-ticketNum--)+"票，剩余："+ticketNum);
        }
    }
}
